package com.cqie.hospitalescort.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cqie.hospitalescort.DTO.AuthUser;
import com.cqie.hospitalescort.DTO.EscortDTO;
import com.cqie.hospitalescort.DTO.IdDTO;
import com.cqie.hospitalescort.entity.OnlineChatInfo;
import com.cqie.hospitalescort.mapper.OnlineChatInfoMapper;
import com.cqie.hospitalescort.model.Result;
import com.cqie.hospitalescort.service.AdminUserService;
import com.cqie.hospitalescort.utils.Asserts;
import com.cqie.hospitalescort.utils.WebSecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gdy
 * @since 2025-02-18
 */
@RestController
@Api(value = "审核模块", tags = {"审核模块"})
public class AdminUserController extends BaseController {

    @Autowired
    private AdminUserService adminUserService;
    @Autowired
    private OnlineChatInfoMapper messageMapper;

    @PostMapping("/user/save")
    @ApiOperation(value = "成为陪诊员", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
//    @PreAuthorize(value = "hasRole('ROLE_USER')")
    public Result<?> save(@RequestBody EscortDTO escortDTO){

        adminUserService.save(escortDTO);
        return response("ok");
    }

    @GetMapping("/become/escort/info")
    @ApiOperation(value = "成为陪诊员", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
//    @PreAuthorize(value = "hasAnyRole('USER','ADMIN')")
    public Result<?> becomeList(){
        return response(adminUserService.becomeList());
    }

    @GetMapping("/become/escort/cancel")
    @ApiOperation(value = "撤销", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<?> cancel(@RequestParam Long id){
        adminUserService.cancel(id);
        return response();
    }


    @PostMapping("/admin/audit")
    @ApiOperation(value = "审核陪诊员", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PreAuthorize(value = "hasRole('ROLE_ADMIN')")
    public Result<?> auditUser(@RequestBody IdDTO dto){
        adminUserService.auditUser(dto);
        return response("ok");
    }


    @PostMapping("/admin/delete")
    @ApiOperation(value = "删除管理员", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PreAuthorize(value = "hasRole('ROLE_ADMIN')")
    public Result<Void> deleteAdminUser(@RequestBody IdDTO dto){
        adminUserService.deleteAdminUser(dto);
        return response();
    }

    @GetMapping("/message/query")
    @ApiOperation(value = "审核成功消息监听", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<?> hasMessage(){
        AuthUser currentUser = WebSecurityUtil.getCurrentUser();
        OnlineChatInfo onlineChatInfo = messageMapper.selectOne(new LambdaQueryWrapper<OnlineChatInfo>().eq(OnlineChatInfo::getUserId, currentUser.getId()));
        if (Objects.nonNull(onlineChatInfo)){
            messageMapper.deleteById(onlineChatInfo.getId());
        }
        return response(onlineChatInfo);
    }

}
